package org.mparts.server;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpSession;

import org.mparts.client.MpartsService;
import org.mparts.server.utils.PersistUtils;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class MpartsServlet extends RemoteServiceServlet implements MpartsService {
	
	private Logger log = Logger.getLogger( MpartsServlet.class.getName() );
	
	public MpartsServlet() {
		// TODO Auto-generated constructor stub
	}

	public MpartsServlet(Object delegate) {
		super(delegate);
		// TODO Auto-generated constructor stub
	}
	
	public String getUploadUrl( String url ) {
		BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
		return blobstoreService.createUploadUrl(url);
	}
	
	public List<String> getUser(String url) {
		return PersistUtils.getUserInfo(url);
	}

	@Override
	public List<String> getLoadCatalogResult() {
		ArrayList<String> result = new ArrayList<String>();
		if (PersistUtils.isUserManager()) {
			HttpSession session = this.getThreadLocalRequest().getSession();
			String ex = (String) session.getAttribute(UploadServlet.KEY_LOAD_CATALOG);
			if ( ex != null ) {
				result.add(ex);
			}
			session.removeAttribute(UploadServlet.KEY_LOAD_CATALOG);
		}
		return result;
	}

}
